package com.dongnao.jack.rmi.impl;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RmiUtil {

    public static void startService(String hostName, String port, String id, SoaRemote sr) {
        try {
            LocateRegistry.createRegistry(Integer.parseInt(port));

            Naming.bind("rmi://" + hostName + ":" + port + "/" + id, sr);

        }
        catch (NumberFormatException | RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (AlreadyBoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static SoaRemote getRmiRegisterInvocation(String hostName, String port, String id) {

        try {
            return (SoaRemote) Naming.lookup("rmi://" + hostName + ":" + port + "/" + id);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

}
